<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="user_id" label="用户ID" required>
        <uni-easyinput placeholder="下单用户ID" v-model="formData.user_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="order_type" label="订单类型">
        <uni-data-checkbox v-model="formData.order_type" :localdata="formOptions.order_type_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="order_status" label="订单状态" required>
        <uni-data-checkbox v-model="formData.order_status" :localdata="formOptions.order_status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="pay_status" label="支付状态">
        <uni-data-checkbox v-model="formData.pay_status" :localdata="formOptions.pay_status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="address_id" label="收货地址ID">
        <uni-easyinput placeholder="收货地址ID，关联用户地址表" v-model="formData.address_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="order_items" label="订单商品">
        <uni-data-checkbox :multiple="true" v-model="formData.order_items"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="order_tradecodes" label="商品tradecode">
        <uni-data-checkbox :multiple="true" v-model="formData.order_tradecodes"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="shop_groups" label="店铺分组">
        <uni-data-checkbox :multiple="true" v-model="formData.shop_groups"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="total_amount" label="商品总金额" required>
        <undefined v-model="formData.total_amount"></undefined>
      </uni-forms-item>
      <uni-forms-item name="shipping_fee" label="运费">
        <undefined v-model="formData.shipping_fee"></undefined>
      </uni-forms-item>
      <uni-forms-item name="discount_amount" label="优惠金额">
        <undefined v-model="formData.discount_amount"></undefined>
      </uni-forms-item>
      <uni-forms-item name="final_amount" label="实付金额" required>
        <undefined v-model="formData.final_amount"></undefined>
      </uni-forms-item>
      <uni-forms-item name="payment_method" label="支付方式">
        <uni-easyinput placeholder="支付方式：wxpay-微信支付，alipay-支付宝，appleiap-苹果支付" v-model="formData.payment_method"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="pay_time" label="支付时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.pay_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="ship_time" label="发货时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.ship_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="receive_time" label="收货时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.receive_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="finish_time" label="完成时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.finish_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="cancel_time" label="取消时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.cancel_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="cancel_reason" label="取消原因">
        <uni-easyinput placeholder="订单取消原因" v-model="formData.cancel_reason"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="remark" label="订单备注">
        <uni-easyinput placeholder="订单备注信息" v-model="formData.remark" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="create_time" label="创建时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.create_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="expire_time" label="过期时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.expire_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="evaluate_status" label="评价状态">
        <uni-data-checkbox v-model="formData.evaluate_status" :localdata="formOptions.evaluate_status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/intangible_userorders.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'intangible_userorders';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "user_id": "",
        "order_type": 0,
        "order_status": 0,
        "pay_status": 0,
        "address_id": "",
        "order_items": [],
        "order_tradecodes": [],
        "shop_groups": [],
        "total_amount": null,
        "shipping_fee": 0,
        "discount_amount": 0,
        "final_amount": null,
        "payment_method": "",
        "pay_time": null,
        "ship_time": null,
        "receive_time": null,
        "finish_time": null,
        "cancel_time": null,
        "cancel_reason": "",
        "remark": "",
        "create_time": null,
        "expire_time": null,
        "evaluate_status": 0
      }
      return {
        formData,
        formOptions: {
          "order_type_localdata": [
            {
              "text": "全新商品",
              "value": 0
            },
            {
              "text": "转售商品",
              "value": 1
            },
            {
              "text": "混合订单",
              "value": 2
            }
          ],
          "order_status_localdata": [
            {
              "text": "待付款",
              "value": 0
            },
            {
              "text": "待发货",
              "value": 1
            },
            {
              "text": "待收货",
              "value": 2
            },
            {
              "text": "待评价",
              "value": 3
            },
            {
              "text": "已完成",
              "value": 4
            },
            {
              "text": "已取消",
              "value": 5
            },
            {
              "text": "退款中",
              "value": 6
            },
            {
              "text": "已退款",
              "value": 7
            }
          ],
          "pay_status_localdata": [
            {
              "text": "未支付",
              "value": 0
            },
            {
              "text": "已支付",
              "value": 1
            },
            {
              "text": "部分退款",
              "value": 2
            },
            {
              "text": "全额退款",
              "value": 3
            }
          ],
          "evaluate_status_localdata": [
            {
              "text": "未评价",
              "value": 0
            },
            {
              "text": "已评价",
              "value": 1
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
